<?php


class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin',
            'RSS Picker Settings',
            'manage_options',
            'my-setting-admin',
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h2>RSS Picker Settings</h2>
            <form id="grabForm" method="post" action="<?php echo plugins_url()?>/rss-picker/misc/rss_grabber.php">
                <?php submit_button("Fetch posts"); ?>
                <div id="fetchProcessContainer">
                    <span id="fetchPostsMessage">Please, wait until all posts are fetched. It may take several minutes</span>
                    <img id="fetchPostsPreloader" src="<?php echo plugins_url()?>/rss-picker/images/preloader.gif" alt="preloader" />
                    <a id="stopFetchButton" class="button button-primary" href="javascript:void(0)">Stop fetch</a>
                </div>
                <p id="fetchSuccessMessage"></p>
            </form>
        </div>
        <h3>RSS Sources List</h3>
        <table class="sources_table">
          <tr>
            <th>Rss source url</th>
            <th>Delete source</th>
          </tr>
        <?php
        $allRssInfo = RssInfoManager::GetAllSourceInfo();
        $optionsUrl = plugins_url() . "/rss-picker/options.php";
        foreach ($allRssInfo as $sourceInfo)
        {
            $sourceUrl = $sourceInfo->GetSourceUrl();
            $rssInfoId = $sourceInfo->GetRssInfoId();
            $deleteUrl = $optionsUrl . "?action=delete&id=" . $rssInfoId;
            ?>
                <tr>
                  <td>
                    <span class="source_url" contenteditable="true"><?= $sourceUrl ?></span>
                    <input class="rss_info_id" type="hidden" value="<?= $rssInfoId ?>" />
                  </td>
                  <td>
                    <a class="delete_source" href="<?= $deleteUrl ?>" title="delete">delete</a>
                  </td>
                </tr>
            <?php
        }
        echo "</table>";
        echo "<input id=\"optionsUrl\" type=\"hidden\" value=\"$optionsUrl\" />";
        ?>
            <h3>Add RSS Source</h3>
            <form id="addSourceForm" method="post" action="<?php echo $optionsUrl ?>">
                <input type="text" value="" name="source_url" />
                <input type="hidden" name="action" value="insert" />
                <?php submit_button("Add source"); ?>
            </form>
        <?php

        // Set class property
        $this->options = get_option( 'rss_pckr_option' );
        ?>
        <div class="wrap">
            <form method="post" action="options.php">
                <?php
                // This prints out all hidden setting fields
                settings_fields( 'my_option_group' );
                do_settings_sections( 'my-setting-admin' );
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {
        register_setting(
            'my_option_group', // Option group
            'rss_pckr_option', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'Fetch Settings', // Title
            array( $this, 'print_section_info' ), // Callback
            'my-setting-admin' // Page
        );

        add_settings_field(
            'max_posts_per_resource', // ID
            'Max posts per resource', // Title
            array( $this, 'max_posts_per_resource_callback' ), // Callback
            'my-setting-admin', // Page
            'setting_section_id' // Section
        );

        add_settings_field(
            'max_posts_per_fetch',
            'Max posts per fetch',
            array( $this, 'max_posts_per_fetch_callback' ),
            'my-setting-admin',
            'setting_section_id'
        );
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['max_posts_per_resource'] ) )
            $new_input['max_posts_per_resource'] = absint( $input['max_posts_per_resource'] );

        if( isset( $input['max_posts_per_fetch'] ) )
            $new_input['max_posts_per_fetch'] = absint( $input['max_posts_per_fetch'] );

        return $new_input;
    }

    /**
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    /**
     * Get the settings option array and print one of its values
     */
    public function max_posts_per_resource_callback()
    {
        printf(
            '<input type="text" id="max_posts_per_resource" name="rss_pckr_option[max_posts_per_resource]" value="%s" />',
            isset( $this->options['max_posts_per_resource'] ) ? esc_attr( $this->options['max_posts_per_resource']) : ''
        );
    }

    /**
     * Get the settings option array and print one of its values
     */
    public function max_posts_per_fetch_callback()
    {
        printf(
            '<input type="text" id="max_posts_per_fetch" name="rss_pckr_option[max_posts_per_fetch]" value="%s" />',
            isset( $this->options['max_posts_per_fetch'] ) ? esc_attr( $this->options['max_posts_per_fetch']) : ''
        );
    }
}